import { SendContexts } from './sendContext';
let _SendContexts = SendContexts;
/**
 * 拷贝下面两行到 mock/index.js中,即可支持客户端虚拟数据、免服务端连接
 * import SendContextMock from './mdp/arc/send/sendContextMock';
 * SendContextMock.bootstrap(mock);
 */
export default {
  /**
   * mock bootstrap
   */
  bootstrap(mock) {    
  
    //获取列表 模糊查询、根据关键字查询
    mock.onGet('/mdp/arc/send/sendContext/listKey').reply(config => {
      let params = config.params;
      let mockSendContexts = _SendContexts.filter(sendContext => {
      	if(params.oaContext||params.oaContext==''){ if( sendContext.oaContext.indexOf(params.oaContext)>=0) return true;}
      	if(params.mainTitle||params.mainTitle==''){ if( sendContext.mainTitle.indexOf(params.mainTitle)>=0) return true;}
      	if(params.mainAbstract||params.mainAbstract==''){ if( sendContext.mainAbstract.indexOf(params.mainAbstract)>=0) return true;}
      	if(params.shortMsgContext||params.shortMsgContext==''){ if( sendContext.shortMsgContext.indexOf(params.shortMsgContext)>=0) return true;}
      	if(params.weixinContext||params.weixinContext==''){ if( sendContext.weixinContext.indexOf(params.weixinContext)>=0) return true;}
      	if(params.otherContext||params.otherContext==''){ if( sendContext.otherContext.indexOf(params.otherContext)>=0) return true;}
      	if(params.sendPlanId||params.sendPlanId==''){ if( sendContext.sendPlanId.indexOf(params.sendPlanId)>=0) return true;}
      	if(params.emailContext||params.emailContext==''){ if( sendContext.emailContext.indexOf(params.emailContext)>=0) return true;}
        return false;
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            data: mockSendContexts
          }]);
        }, 1000);
      });
    });
    
    

    //获取列表（分页/不分页）
    mock.onGet('/mdp/arc/send/sendContext/list').reply(config => {
      let params = config.params;
      let mockSendContexts = _SendContexts.filter(sendContext => {
      	var isTrue=false;
      	if(params.oaContext||params.oaContext==''){if(sendContext.oaContext.indexOf(params.oaContext)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.mainTitle||params.mainTitle==''){if(sendContext.mainTitle.indexOf(params.mainTitle)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.mainAbstract||params.mainAbstract==''){if(sendContext.mainAbstract.indexOf(params.mainAbstract)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.shortMsgContext||params.shortMsgContext==''){if(sendContext.shortMsgContext.indexOf(params.shortMsgContext)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.weixinContext||params.weixinContext==''){if(sendContext.weixinContext.indexOf(params.weixinContext)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.otherContext||params.otherContext==''){if(sendContext.otherContext.indexOf(params.otherContext)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.sendPlanId||params.sendPlanId==''){if(sendContext.sendPlanId.indexOf(params.sendPlanId)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.emailContext||params.emailContext==''){if(sendContext.emailContext.indexOf(params.emailContext)>=0){isTrue=true;}else{isTrue=false;}}
      	return isTrue;
	  });
      if(!config.params.currentPage){ 
	      return new Promise((resolve, reject) => {
	        setTimeout(() => {
	          resolve([200, {
	            data: mockSendContexts
	          }]);
	        }, 1000);
	      });
      }
    
      let {pageSize,currentPage, total} = config.params;
      total = mockSendContexts.length;
      mockSendContexts = mockSendContexts.filter((sendContext, index) => index < pageSize * currentPage && index >= pageSize * (currentPage - 1));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            pageInfo: {
            	total:total
            },
            data: mockSendContexts,
            tips:{
            	isOk:true,
            	msg:'查询成功'
            }
          }]);
        }, 1000);
      });
    });

    //删除
    mock.onPost('/mdp/arc/send/sendContext/del').reply(config => {
      var params = JSON.parse( config.data );
      let {  sendPlanId  } = params;
      _SendContexts = _SendContexts.filter(sendContext => sendContext.sendPlanId !== sendPlanId);
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //批量删除
    mock.onPost('/mdp/arc/send/sendContext/batchDel').reply(config => {
      var params = JSON.parse( config.data );
      _SendContexts = _SendContexts.filter(sendContext => !params.includes(sendContext.sendPlanId));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
           tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //编辑
    mock.onPost('/mdp/arc/send/sendContext/edit').reply(config => {
      var params = JSON.parse( config.data );
      let { oaContext,mainTitle,mainAbstract,shortMsgContext,weixinContext,otherContext,sendPlanId,emailContext } = params;
      _SendContexts.some(sendContext => {
        if (sendContext.sendPlanId === sendPlanId) {
			sendContext.oaContext = oaContext;
			sendContext.mainTitle = mainTitle;
			sendContext.mainAbstract = mainAbstract;
			sendContext.shortMsgContext = shortMsgContext;
			sendContext.weixinContext = weixinContext;
			sendContext.otherContext = otherContext;
			sendContext.sendPlanId = sendPlanId;
			sendContext.emailContext = emailContext
          return true;
        }
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'编辑成功'
            }
          }]);
        }, 500);
      });
    });

    //新增
    mock.onPost('/mdp/arc/send/sendContext/add').reply(config => {
      var params = JSON.parse( config.data );
      let { oaContext,mainTitle,mainAbstract,shortMsgContext,weixinContext,otherContext,sendPlanId,emailContext } = params;
      _SendContexts.push({
		oaContext:oaContext,
		mainTitle:mainTitle,
		mainAbstract:mainAbstract,
		shortMsgContext:shortMsgContext,
		weixinContext:weixinContext,
		otherContext:otherContext,
		sendPlanId:sendPlanId,
		emailContext:emailContext
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'新增成功'
            }
          }]);
        }, 500);
      });
    });

  }
};